//
//  ViewController.m
//  gradientlayer
//
//  Created by zmj27404 on 16/2/17.
//  Copyright © 2016年 zmj27404. All rights reserved.
//

#import "ViewController.h"
#import "gradientLayerView.h"
#import "SecondViewController.h"

@interface ViewController ()
{
    gradientLayerView *gradientView;
    NSTimer *timer;
    NSUInteger startYView;
    NSUInteger count;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    count = 0;
    NSMutableArray *points = @[].mutableCopy;
    NSUInteger maxX = 300;
    NSUInteger lengthY = 20;
    startYView = 2000 + lengthY / 3;
    NSUInteger pointCount = arc4random() % (maxX / 2) + 0.75 * maxX;
    for (NSInteger i = 0; i < pointCount; i++) {
        @autoreleasepool {
            [points addObject:@(arc4random()%(lengthY/2) + startYView)];
        }
    }
    NSLog(@"points.count = %ld, lastCount = %@", points.count, [points lastObject]);
    gradientView = [[gradientLayerView alloc] initWithFrame:CGRectMake(10, 60, CGRectGetWidth([UIScreen mainScreen].bounds) - 2 * 10, CGRectGetHeight([UIScreen mainScreen].bounds) - 60 - 10) andPoints:points maxCount:maxX - 250];
    
    UIColor *yellor = [UIColor yellowColor];
    yellor = [yellor colorWithAlphaComponent:0.3];
    gradientView.gradientColor = @[(__bridge id)yellor.CGColor, (__bridge id)[UIColor blackColor].CGColor];
    [gradientView show];
    [self.view addSubview:gradientView];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateGradientView) userInfo:nil repeats:YES];
    [timer setFireDate:[NSDate distantPast]];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 20, 100, 40);
    [btn setTitle:@"下一页" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (timer) {
        [timer setFireDate:[NSDate distantPast]];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (timer) {
        [timer setFireDate:[NSDate distantFuture]];
    }
}

- (void)nextPage:(UIButton *)btn {
    SecondViewController *vc = [[SecondViewController alloc] init];
    vc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)updateGradientView{
    NSNumber *point = @(startYView + arc4random()%5);
    BOOL flag = count >= 30;
    if (flag){
        count = 0;
        NSLog(@"30s -> %@", point);
    }
    count++;
#if 1
    [gradientView addPoint:point isNew:flag];
#endif
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
